﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CopyFile
{
    public partial class Progress : Form
    {
        private BackgroundWorker backgroundWorker;
        public Progress()
        {
            InitializeComponent();
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.WorkerSupportsCancellation = true;
            backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
         
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            backgroundWorker.CancelAsync();
        }

        public void CopyFile(CopyFileArgs args)
        {
            this.Show();
            backgroundWorker.RunWorkerAsync(args);            
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
                MessageBox.Show("Copy Aborted");
            else
                pbCopyProgress.Value = 100;
            this.Close();
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {

            CopyFileArgs args = (CopyFileArgs) e.Argument;
            BackgroundWorker worker = (BackgroundWorker) sender;

            try
            {
                System.IO.FileInfo fileinInfo = new System.IO.FileInfo(args.Source);
                long fileSize = fileinInfo.Length;
                using (System.IO.FileStream sourceStream = new System.IO.FileStream(args.Source, FileMode.Open))
                {
                    using (System.IO.FileStream destinationStream = new FileStream(args.Destination, FileMode.CreateNew)
                        )
                    {
                        int count = 512*1024;
                        byte[] fileData = new byte[count];
                        int offset = 0;
                        int bytesRead;
                        int bytesWritten = 0;
                        do
                        {
                            bytesRead = sourceStream.Read(fileData, offset, count);
                            if (bytesRead > 0)
                            {
                                destinationStream.Write(fileData, offset, bytesRead);
                                bytesWritten += bytesRead;
                                worker.ReportProgress((int) (((1.0*bytesWritten)/fileSize)*100));
                            }
                        } while (bytesRead > 0 && worker.CancellationPending == false);
                    if(worker.CancellationPending)
                        {
                            e.Cancel = true;
                        }
                    }
                }
            }catch
                (Exception ex)
            {
                
                worker.CancelAsync();
                e.Cancel = true;
            }
        }

        void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
            this.pbCopyProgress.Value = e.ProgressPercentage;
            System.Diagnostics.Debug.Write(e.ProgressPercentage);
        }
    }
}
